// scalaxx - Scala XML eXtensions
// (c) 2009-2010, Normen Müller, normen.mueller@gmail.com
// $Id: XPath.scala 78 2010-05-11 21:09:38Z normen.mueller $
package scalaxx.parsing.path

import scalaxx.path._
/** This object provides a simple interface to the XPath1.0 parser class.
 *
 * <dl>
 *   <dt>Union</dt><dd>Constructor of XPath union expressions</dd>
 *   <dt>Root</dt><dd>Constructor of XPath root expressions</dd>
 *   <dt>Path</dt><dd>Constructor of XPath location expressions</dd>
 *   <dt>Filter</dt><dd>Constructor of XPath filter expressions</dd>
 *   <dt>Axis</dt><dd>Constructor of XPath axis expressions</dd>
 *   <dt>*</dt><dd>Asterisk</dd>
 *   <dt>Type</dt><dd>Constructor of XPath node type expressions</dd>
 *   <dt>NCName</dt><dd>Constructor of XPath NCName expressions</dd>
 *   <dt>QName</dt><dd>Constructor of XPath QName expressions</dd>
 *   <dt>Fun</dt><dd>Constructor of XPath function expressions</dd>
 *   <dt>Val</dt><dd>Constructor of XPath value (string, number) expressions</dd>
 *   <dt>Var</dt><dd>Constructor of XPath variable reference expressions</dd> *   
 * </dl>
 * 
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.3
 */
object XPathExpressionParser extends Parser {  
  def parse(input: String): Expression = {
    val expand = (in: String) => input.trim replaceAll ("//", "/descendant-or-self::node()/") replaceAll ("@", "attribute::") replaceAll ("""\.\.""", "parent::node()") replaceAll
      //@todo `.' should be handled better
      ("""\./""", "self::node()/") replaceAll ("""\.,""", "self::node(),") replaceAll ("""\.=""", "self::node()=") replaceAll ("""[\.]$""", "self::node()")
    phrase(expr)(new lexical.Scanner(expand(input))) match {
      case Success(result, _) => result
      case n@_                => throw new Exception("XPath parsing error: " + n)
    }
  }  
}

/** This object provides a simple interface to the XPath1.0 parser class.
 *
 * <dl>
 *   <dt>Root</dt><dd>Constructor of XPath root expressions</dd>
 *   <dt>Path</dt><dd>Constructor of XPath location expressions</dd>
 *   <dt>Axis</dt><dd>Constructor of XPath axis expressions</dd>
 *   <dt>*</dt><dd>Asterisk</dd>
 * </dl>
 * 
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.3
 */
object XPathLocationPathParser extends Parser {  
  //@todo evaluate if ``expand'' is necessary
  def parse(input: String): LocationPath = phrase(locPath)(new lexical.Scanner(input)) match {
    case Success(result, _) => result
    case n@_                => throw new Exception("XPath parsing error: " + n)
  }  
}
